package com.sanyang.pda.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.sanyang.pda.bean.ReasonListBean;
import com.wangshen.base.dialog.base.BaseDialog;
import com.wangshen.base.util.ToastUtils;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddCauseDialog extends BaseDialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_num_title)
    TextView tvNumTitle;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_tray)
    Spinner tvTray;
    @BindView(R.id.iv_scan)
    TextView ivScan;
    OnCauseclick onCauseclick;
    List<ReasonListBean> reasonListBeans=new ArrayList<>();
    private List<String> list =new ArrayList<>();
    private String reason;
    public AddCauseDialog(@NonNull Context context,OnCauseclick onCauseclick) {
        super(context);
        this.onCauseclick=onCauseclick;
    }
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            //return ss.length;
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView=new TextView(getContext());
            //textView.setText(ss[position]);
            textView.setText(list.get(position));
            return textView;
        }
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    public void show(List<ReasonListBean> reasonListBeans) {
        super.show();
        if (reasonListBeans == null){
            return;
        }
        this.reasonListBeans = reasonListBeans;
        for (ReasonListBean item : reasonListBeans){
            list.add(item.getDictLabel());
        }
        BaseAdapter adapter =new MyAdapter();//adapter适配器
        tvTray.setAdapter(adapter);
        //可以监听器看看
        tvTray.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //选择了不同的选项，调用这个
               reason=list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //在下拉选项种选择了本来选的东西，也就是说没改变选项，调用这个

            }
        });

    }

    @Override
    public int getView() {
        return R.layout.dialog_add_cause;
    }

    @OnClick({R.id.iv_close, R.id.iv_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.iv_scan:
                if (TextUtils.isEmpty(tvNum.getText())){
                    ToastUtils.showToast(getContext(),getContext().getString(R.string.qualified_num)+getContext().getString(R.string.not_be_empty));
                    return;
                }

                if (TextUtils.isEmpty(reason)){
                    ToastUtils.showToast(getContext(),getContext().getString(R.string.unqualified_cause)+getContext().getString(R.string.not_be_empty));
                    return;
                }
                if (onCauseclick != null){
                    onCauseclick.onCauseClick(tvNum.getText().toString(),reason);
                    dismiss();
                }
                break;
        }
    }
    public interface OnCauseclick{
        void onCauseClick(String unqualifiedNum,String reason);
    }
}
